package com.utf999.qiangmanger.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.IStorefrontDAO;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.entity.orm.StorefrontEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.IAdminUserService;
import com.utf999.qiangmanger.service.iface.IStorefrontService;
import com.utf999.qiangmanger.utils.MD5Utils;

/**
 * @author yinwenjie
 *
 */
@Component("StorefrontServiceImpl")
public class StorefrontServiceImpl extends AbstractService<StorefrontEntity, IStorefrontDAO> implements IStorefrontService {

	/**
	 * 
	 */
	@Autowired
	private IStorefrontDAO storefrontDAO;
	
	@Autowired
	private IAdminUserService adminUserService;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected IStorefrontDAO getBaseDAO() {
		return this.storefrontDAO;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStorefrontService#queryStorefrontList(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<StorefrontEntity> queryStorefrontList(String companyid , Integer status) throws BizException {
		if(StringUtils.isEmpty(companyid)) {
			return null;
		}
		
		return this.storefrontDAO.queryStorefrontList(companyid , status);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStorefrontService#queryStorefrontByuserid(java.lang.String)
	 */
	@Override
	public StorefrontEntity queryStorefrontByuserid(String adminuserid) throws BizException {
		if(StringUtils.isEmpty(adminuserid)) {
			return null;
		}
		
		return this.storefrontDAO.queryStorefrontByuserid(adminuserid);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStorefrontService#appendStorefront(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public StorefrontEntity appendStorefront(String companyid, String storeName, String storeadd, String storephone, String storeadminname, String storeadminpassword) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(storeName)
			|| StringUtils.isEmpty(storephone) || StringUtils.isEmpty(storeadminname)
			|| StringUtils.isEmpty(storeadminpassword)) {
			
			throw new BizException("参数不正常，请检查！", ResponseCode.E404);
		}
		
		/*
		 * 经过参数的有效性判断，到这里的信息都是有效的：
		 * 1、首先组装一个store实体，并关联company的uid，进行添加
		 * 2、然后添加管理员信息，包括用户名和密码信息（以及管理员的角色级别）
		 * */
		//==========1
		StorefrontEntity storefront = new StorefrontEntity();
		CompanyEntity company = new CompanyEntity();
		company.setUid(companyid);
		storefront.setCompany(company);
		storefront.setCreateTime(new Date());
		storefront.setStatus(1);
		storefront.setStoreAddress(storeadd);
		storefront.setStoreName(storeName);
		storefront.setStorePhone(storephone);
		this.storefrontDAO.insert(storefront);
		
		//==========2
		AdminUserEntity admin = new AdminUserEntity();
		//设置角色为“店面管理员角色”
		admin.setAdminLevel(3);
		admin.setCompany(company);
		admin.setEncodePassword(MD5Utils.encoder(storeadminpassword));
		admin.setJoinTime(new Date());
		//最后登录时间
		Date lastlogTime = new Date();
		lastlogTime.setTime(0l);
		admin.setLastlogTime(lastlogTime);
		admin.setStatus(1);
		admin.setStorefront(storefront);
		admin.setUserName(storeadminname);
		this.adminUserService.insert(admin);
		
		return storefront;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IStorefrontService#updateStorefront(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void updateStorefront(String storeid, String storeName, String storephone, String storeadd) throws BizException {
		if(StringUtils.isEmpty(storeid) || StringUtils.isEmpty(storeName)
			|| StringUtils.isEmpty(storephone)) {
			
			throw new BizException("参数不正常，请检查！", ResponseCode.E404);
		}
		
		StorefrontEntity storefront = this.storefrontDAO.getEntity(storeid);
		storefront.setStoreName(storeName);
		storefront.setStorePhone(storephone);
		storefront.setStoreAddress(storeadd);
		this.update(storefront);
	}
}
